﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using ByteSite.Common;

namespace ByteSite.BLL
{
    /// <summary>
    ///会员金额日志表
    /// </summary>
    public partial class user_amount_log
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.Data data;

        public user_amount_log()
        {
            data = new DAL.Data();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_user_amount_log>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_user_amount_log model)
        {
            try
            {
                data.BeginTran();
                model.id= data.Insert(model);

                data.UpdateColumns<Entitys.dt_users>(x => x.amount == x.amount + model.value, x => x.id == model.user_id);
                data.CommitTran();
            }
            catch
            {
                data.RollbackTran();
                return 0;
            }
            return model.id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_user_amount_log model)
        {

            try
            {
                data.BeginTran();
                data.Update(model);

                data.UpdateColumns<Entitys.dt_users>(x => x.amount == x.amount + model.value, x => x.id == model.user_id);
                data.CommitTran();
            }
            catch
            {
                data.RollbackTran();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return data.Delete<Entitys.dt_user_amount_log>(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_user_amount_log GetModel(int id)
        {
            return data.Get<Entitys.dt_user_amount_log>(id);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_user_amount_log> GetList(int Top, string strWhere, string filedOrder)
        {
            return data.List<Entitys.dt_user_amount_log>(Top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_user_amount_log> GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return data.List<Entitys.dt_user_amount_log>(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(int user_id, string user_name, decimal value, string remark)
        {
            Entitys.dt_user_amount_log model = new Entitys.dt_user_amount_log();
            model.user_id = user_id;
            model.user_name = user_name;
            model.value = value;
            model.remark = remark;
            return data.Insert(model);
        }

        /// <summary>
        /// 根据用户名删除一条数据
        /// </summary>
        public bool Delete(int id, string user_name)
        {
            return data.Delete<Entitys.dt_user_amount_log>(x => x.id == id && x.user_name == user_name)>0;
        }
        #endregion
    }
}